//
//  CLmyViewController.m
//  I-want-go
//
//  Created by dllo on 15/10/21.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "CLmyViewController.h"
#import "UMSocial.h"
#import "MobClick.h"
#import "CLmyController.h"
#import "UMSocial.h"
#import "MobClick.h"
#import "CLtool.h"
#import "CLmyTableCell.h"
//#import "CLSingleton.h"
#import "CLscreen.h"
#import "CLtakePhoto.h"
#import "CLerViewController.h"
#import "CLfunctionViewController.h"
@interface CLmyViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)UITableView *mytableView;
@property(nonatomic, retain)UIImageView *myView;
//@property(nonatomic, retain)UILabel *funsLabel;
//@property(nonatomic, retain)UILabel *seeLabel;
@property(nonatomic, retain)UIImageView *phoneImage;
@property(nonatomic, retain)UMSocialSnsPlatform *snsPlatform;

@property(nonatomic, retain)NSArray *arr;
@property(nonatomic, retain)UIImageView *erImage;
@end

@implementation CLmyViewController
- (void)dealloc {
    [_erImage release];
    [_arr release];
    [_phoneImage release];
    [_mytableView release];
    [_myView release];
//    [_seeLabel release];
//    [_funsLabel release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的";
    
    self.mytableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375 * SWIDTH, 603 * SHEIGHT)];
    [self.view addSubview:self.mytableView];
    [_mytableView release];
    self.mytableView.tableHeaderView = self.myView;
    
    self.myView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375 * SWIDTH, 230 * SHEIGHT)];
     self.myView.image = [UIImage imageNamed:@"good.png"];
    self.myView.userInteractionEnabled = YES;
    self.mytableView.tableHeaderView = self.myView;
    [_myView release];
    
//    self.funsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 210 * SHEIGHT, 187 * SWIDTH, 20 * SHEIGHT)];
//    self.funsLabel.backgroundColor = [UIColor blackColor];
//    self.funsLabel.textAlignment = NSTextAlignmentCenter;
//    self.funsLabel.textColor = [UIColor whiteColor];
//    [self.myView addSubview:self.funsLabel];
//    self.funsLabel.text = @"粉丝 0";
//    [_funsLabel release];
    
    self.mytableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//
//    self.seeLabel = [[UILabel alloc] initWithFrame:CGRectMake(187 * SWIDTH, 210 * SHEIGHT, 187 * SWIDTH, 20 * SHEIGHT)];
//    self.seeLabel.backgroundColor = [UIColor blackColor];
//    self.seeLabel.textAlignment = NSTextAlignmentCenter;
//    self.seeLabel.textColor = [UIColor whiteColor];
//    [self.myView addSubview:self.seeLabel];
//    self.seeLabel.text = @"关注 0";
//    [_seeLabel release];
    
    
    self.mytableView.dataSource = self;
    self.mytableView.delegate = self;

    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    CLmyTableCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[CLmyTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
    }
    self.arr = [NSArray array];
    self.arr= @[@"我的收藏", @"夜间模式", @"清除缓存",@"更换背景"];
    self.mytableView.rowHeight = 50 * SHEIGHT;
    cell.textLabel.text = self.arr[indexPath.row];
    //    无色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
//        if ([CLSingleton shareSingleton].islog == YES) {
            CLmyController *coloVC = [[CLmyController alloc] init];
            [self.navigationController pushViewController:coloVC animated:YES];
//        }
//        else {
//            UIAlertController *aleryController = [UIAlertController alertControllerWithTitle:@"没有登录" message:@"请登录" preferredStyle:UIAlertControllerStyleAlert];
//            [self presentViewController:aleryController animated:YES completion:nil];
//            UIAlertAction *leftAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
//            
//            [aleryController addAction:leftAction];
//        }
    }
    
//    else if (indexPath.row == 1) {
//        //        当用户使用第三方账号（如新浪微博）登录时，可以这样统计：
//        
//        [MobClick profileSignInWithPUID:@"playerID" provider:@"WB"];
//        
//        //     在新浪微博登录按钮中实现下面的方法
//        self.snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
//        
//        self.snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
//            
//            //          获取微博用户名、uid、token等
//            
//            if (response.responseCode == UMSResponseCodeSuccess) {
//                [CLSingleton shareSingleton].islog = YES;
//
//                UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
//                
//                NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
//                
//                self.phoneImage = [[UIImageView alloc] initWithFrame:CGRectMake(150 * SWIDTH, 20 * SHEIGHT, 75 * SWIDTH, 75 * SHEIGHT)];
//                self.phoneImage.layer.cornerRadius = 25;
//                self.phoneImage.layer.masksToBounds = YES;
//                [self.myView addSubview:self.phoneImage];
//                //            NSLog(@"%@", self.strImage);
//                [_phoneImage release];
//                [self.phoneImage sd_setImageWithURL:[NSURL URLWithString:snsAccount.iconURL]];
//                
//                self.title = snsAccount.userName;
//                
//        }});
//        
//        //        在授权完成后调用获取用户信息的方法
//        
//        //获取accestoken以及新浪用户信息，得到的数据在回调Block对象形参respone的data属性
//        [[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToSina  completion:^(UMSocialResponseEntity *response){
//            NSLog(@"SnsInformation is %@",response.data);
//        }];
//
//    }
    
    else if (indexPath.row == 2) {
        NSFileManager *manager = [NSFileManager defaultManager];
        NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        NSLog(@"%@", cachePath);
        
        CGFloat cacheSize = [self folderSizeAtPath:cachePath];
        BOOL result = [manager removeItemAtPath:cachePath error:nil];
        if (result) {
            UIAlertController *aleryController = [UIAlertController alertControllerWithTitle:@"清除缓存" message:[NSString stringWithFormat:@"%@%.2fM", @"清除", cacheSize] preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:aleryController animated:YES completion:nil];
            UIAlertAction *leftAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
            [aleryController addAction:leftAction];
        } else {
            UIAlertController *aleryController = [UIAlertController alertControllerWithTitle:@"清除缓存" message:@"缓存已经清空" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:aleryController animated:YES completion:nil];
            UIAlertAction *leftAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
            UIAlertAction *rightAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
            [aleryController addAction:leftAction];
            [aleryController addAction:rightAction];
        }
    }
//    else if (indexPath.row == 4) {
//        CLfunctionViewController *funVC = [[CLfunctionViewController alloc] init];
//        [self.navigationController pushViewController:funVC animated:YES];
//        
//    }
//    else if (indexPath.row == 7) {
//        if ([CLSingleton shareSingleton].islog == YES) {
//            
//            UIAlertController *aleryController = [UIAlertController alertControllerWithTitle:@"退出登录" message:@"确定退出" preferredStyle:UIAlertControllerStyleAlert];
//            [self presentViewController:aleryController animated:YES completion:nil];
//            UIAlertAction *leftAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
//                [[UMSocialDataService defaultDataService] requestUnOauthWithType:UMShareToSina  completion:^(UMSocialResponseEntity *response){
//                    NSLog(@"%@", response);
//                    self.title = @"我的";
//                    self.phoneImage.image = nil;
//                }];
//                [CLSingleton shareSingleton].islog = NO;
//                [self.mytableView reloadData];
//            }];
//            UIAlertAction *rightAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
//            [aleryController addAction:leftAction];
//            [aleryController addAction:rightAction];
//            } else {
//            UIAlertController *aleryController = [UIAlertController alertControllerWithTitle:@"没有登录" message:@"请登录" preferredStyle:UIAlertControllerStyleAlert];
//            [self presentViewController:aleryController animated:YES completion:nil];
//            UIAlertAction *leftAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
//            [aleryController addAction:leftAction];
//        }
//    }
    else if (indexPath.row == 1) {
        if (self.view.window.alpha == 1) {
            self.view.window.alpha = 0.5;
        } else {
            self.view.window.alpha = 1;
        }
    }
    
    else if (indexPath.row == 3) {
        [CLtakePhoto sharePicture:^(UIImage *image) {
                NSLog(@"111");
            self.myView.image = image;
            
        }];
    }
//    else if (indexPath.row == 4) {
////        CLerViewController *erVC = [[CLerViewController alloc] init];
////        [self.navigationController pushViewController:erVC animated:YES];
//        UIImage *img = [QRCodeGenerator qrImageForString:@"sky"  imageSize:200];
//        self.myView.image = img;
//    }
}

- (float)folderSizeAtPath:(NSString *)folderPath {
    
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return  0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    
    NSString *fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil) {
        NSString *fileAbsoltePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsoltePath];
    }
    return folderSize / (1024.0 *1024.0);
}

- (long long)fileSizeAtPath:(NSString *)filePath {
    
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]) {
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}












- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
